﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AntdUI;
using static System.ComponentModel.Design.ObjectSelectorEditor;

namespace TPressure
{
    public partial class TemplateManageForm : BaseForm
    {
        #region 属性
        /// <summary>设备
        /// 
        /// </summary>
        private Template obj;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadTemplateList()
        {
            List<Template> list = SqlSugarHelper.Inquireable<Template>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Template> list)
        {
            var antItemList = new List<TemplateItem>();
            foreach (Template item in list)
            {   string first = string.Format("压力:{0}--时长：{1}", item.first_stage_pressure.ToString(),item.first_stage_minute.ToString());
                string second = string.Format("压力:{0}--时长：{1}", item.second_stage_pressure.ToString(), item.second_stage_minute.ToString());
                string third = string.Format("压力:{0}--时长：{1}", item.third_stage_pressure.ToString(), item.third_stage_minute.ToString());
                string rigor = string.Format("压力:{0}--时长：{1}", item.rigor_stage_pressure.ToString(), item.rigor_stage_minute.ToString());
                string intensity = string.Format("压力:{0}--时长：{1}", item.intensity_stage_pressure.ToString(), item.intensity_stage_minute.ToString());
                string edit = "否";
                if (item.edit_enable == 1)
                {
                    edit = "是";
                }
                antItemList.Add(new TemplateItem(item.template_number,item.pipe_position,item.design_pressure,
                    first, second, third, rigor,intensity,edit));
            }
            Table1.DataSource = antItemList;
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Template TemplateWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist(int number)
        {
            obj = TemplateWithNumber(number);
            if (obj == null)
            {
                Alert.Error("模板记录不存在", this);
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region 系统方法
        public TemplateManageForm()
        {
            InitializeComponent();
            this.FullScreen = true;
        }

        private void ManageTemplateForm_Load(object sender, EventArgs e)
        {
            Table1.Height = this.Height - 40;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("template_number","模板编号"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("pipe_position","管道位置"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("design_pressure","设计压力"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("first_stage","第一阶段"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("second_stage","第二阶段"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("third_stage","第三阶段"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("rigor_stage","严密阶段"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("intensity_stage","强度阶段"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("operation","操作"){ Fixed=true,Width="auto",ColAlign=AntdUI.ColumnAlign.Center}
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table1.Spin(config, () =>
            {
                LoadTemplateList();
            }, () =>
            {

            });
        }

        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is TemplateItem data)
            {

                AntdUI.CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                    EditTemplateForm frm = new EditTemplateForm();
                    frm.template_number = data.template_number;
                    frm.Owner = this;
                    frm.ShowDialog();
                }
                else if (link.Id == "delete")
                {
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", "您确定要删除此设备吗？")
                    {
                        OnOk = config =>
                        {
                            bool res = CheckSelectedRowExist(data.template_number);
                            if (!res)
                            {
                                return true;
                            }
                            List<Template> list = new List<Template>();
                            list.Add(obj);
                            int deleteRes = SqlSugarHelper.Deleteable(list);
                            if (deleteRes > 0)
                            {
                                LoadTemplateList();
                            }
                            else
                            {
                                Alert.Error("删除设备失败", this);
                            }
                            return true;
                        },
                    });
                }
            }
        }

        #endregion

    }
}
